package com.emm.yixun.website.listeners;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;

import com.emm.yixun.common.model.ReserveArea;
import com.emm.yixun.common.model.Signed;
import com.emm.yixun.common.model.dto.NeedReceiveRefundDto;
import com.emm.yixun.common.model.dto.NoticeEntity;
import com.emm.yixun.common.model.dto.ReserveAreaDto;
import com.emm.yixun.common.model.enums.AuditStatusEnum;
import com.emm.yixun.common.model.enums.BusinessFlagEnum;
import com.emm.yixun.common.model.enums.HousingFundTypeEnum;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.customer.service.SignedServiceFacade;
import com.emm.yixun.marketing.service.NeedReceiveRefundServiceFacade;
import com.emm.yixun.marketing.service.ReserveAreaServiceFacade;
import com.emm.yixun.system.service.notice.deal.ReserveAreaNoticeFacade;
import com.emm.yixun.website.common.utils.SpringWebContextUtil;
import com.emm.yixun.website.service.ActivitiProcessServiceImpl;

/**
* 类描述： 面积补差流程审批结果
* 创建者： ThinkPad
* 项目名称： emm-website
* 创建时间： 2016年7月13日 下午4:49:19
* 版本号： v1.0
 */
public class ReserveAreaListeners implements Serializable, ExecutionListener{

	private static final long serialVersionUID = 1L;

	@Override
	public void notify(DelegateExecution execution) throws Exception {
		String id=execution.getProcessBusinessKey(); //业务ID
		String processInstanceId=execution.getProcessInstanceId();	//流程ID
		if(getActivitiProcessServiceImpl().isCheckPass(processInstanceId)){
			ReserveAreaDto entity = new ReserveAreaDto();
			entity.setId(new Long(id));
			entity.setAuditStatus(AuditStatusEnum.SUCCESS.getNo());
			entity.setProInsId(processInstanceId);
			getReserveAreaServiceFacadeImpl().updateByPrimaryKey(entity, true);
			Response<ReserveArea> response = getReserveAreaServiceFacadeImpl().findById(new Long(id));
			ReserveArea obj = null;
			if(response.isSuccess()&&response.getEntity()!=null){
				obj = response.getEntity();
				Signed signed = getSignedServiceFacadeImpl().findById(obj.getSignedId()).getEntity();
				NeedReceiveRefundDto needReceiveRefundDto=new NeedReceiveRefundDto();
				needReceiveRefundDto.setRelationId(new Long(id)); //保存面积补差ID
				needReceiveRefundDto.setBusinessType("3");
				needReceiveRefundDto.setTotalReceivablesAmount(obj.getMakeBalance());
				needReceiveRefundDto.setCustomerId(signed.getCustomerId());
				needReceiveRefundDto.setRoomId(signed.getRoomId());
				needReceiveRefundDto.setHousingFundType(HousingFundTypeEnum.FUNDTYPE_23.getNo());
				needReceiveRefundDto.setReceivablesFunds(HousingFundTypeEnum.FUNDTYPE_23.getDes());
				needReceiveRefundDto.setBusinessFlag(BusinessFlagEnum.BUSINESSFLAG_15.getNo());
				needReceiveRefundDto.setReceivablesDate(obj.getReceivableDate());
				needReceiveRefundDto.setPaymentMethod(signed.getPaymentMethod());
				needReceiveRefundDto.setStatus("1");
				needReceiveRefundDto.setPostponedStatus("0");
				if(obj.getMakeBalance().compareTo(new BigDecimal("0"))>0){ //正数  增加应收
					needReceiveRefundDto.setDocType("0");
				}else{
					double makebalance = Math.abs(new Double(obj.getMakeBalance().toString()));
					needReceiveRefundDto.setTotalReceivablesAmount(new BigDecimal(makebalance));
					needReceiveRefundDto.setDocType("1");
				}
				needReceiveRefundDto.setMerchantId(obj.getMerchantId());
				needReceiveRefundDto.setUserCode(obj.getUserCode());
				needReceiveRefundDto.setAccessCode(obj.getAccessCode());
				needReceiveRefundDto.setCreateUserId(obj.getCreateUserId());
				needReceiveRefundDto.setDelFlag("0");
				needReceiveRefundDto.setUpdateUserId(obj.getUpdateUserId());
				needReceiveRefundDto.setUpdateTime(new Date());
				Response<NeedReceiveRefundDto> nrrdSaveResponse=getNeedReceiveRefundServiceFacade().save(needReceiveRefundDto);
				if(!nrrdSaveResponse.isSuccess()){
	    			throw new Exception(nrrdSaveResponse.getResultMessage());
	    		}
			}
			
			/**
			 * （面积补差流程审核通过）推送消息到app
			 * @author ny
			 * @param param noticeType 提醒类型, noticeBusinessId 业务主键Id
			 * @return 【${auditStatus}】房号：${roomName}；客户：${customer};
			 */
			NoticeEntity noticeEntity = new NoticeEntity();
			noticeEntity.setNoticeBusinessId(obj.getSignedId());
			noticeEntity.setNoticeType("10");
			noticeEntity.setProcessInstanceId(processInstanceId);
			getReserveAreaNoticeFacade().createReserveAreaNotice(noticeEntity);
		}else{
			ReserveAreaDto entity = new ReserveAreaDto();
			entity.setId(new Long(id));
			entity.setAuditStatus(AuditStatusEnum.REJECT.getNo());
			getReserveAreaServiceFacadeImpl().updateByPrimaryKey(entity, true);
			
			/**
			 * （面积补差流程审核不通过）推送消息到app
			 * @author ny
			 * @param param noticeType 提醒类型, noticeBusinessId 业务主键Id
			 * @return 【${auditStatus}】房号：${roomName}；客户：${customer};
			 */
			Response<ReserveArea> response = getReserveAreaServiceFacadeImpl().findById(new Long(id));
			if(response.isSuccess()&&response.getEntity()!=null){
				ReserveArea obj_2 = response.getEntity();
				NoticeEntity noticeEntity = new NoticeEntity();
				noticeEntity.setNoticeBusinessId(obj_2.getSignedId());
				noticeEntity.setNoticeType("10");
				noticeEntity.setProcessInstanceId(processInstanceId);
				getReserveAreaNoticeFacade().createReserveAreaNotice(noticeEntity);
			}
		}
	}
	
	private ReserveAreaNoticeFacade getReserveAreaNoticeFacade(){
		return (ReserveAreaNoticeFacade)SpringWebContextUtil.getApplicationContext().getBean("reserveAreaNoticeFacade");
	}
	
	private ActivitiProcessServiceImpl getActivitiProcessServiceImpl(){
		return (ActivitiProcessServiceImpl)SpringWebContextUtil.getApplicationContext().getBean("activitiProcessServiceImpl");
	}
	
	private ReserveAreaServiceFacade getReserveAreaServiceFacadeImpl(){
		return (ReserveAreaServiceFacade)SpringWebContextUtil.getApplicationContext().getBean("reserveAreaServiceFacade");
	}
	
	private SignedServiceFacade getSignedServiceFacadeImpl(){
		return (SignedServiceFacade)SpringWebContextUtil.getApplicationContext().getBean("signedServiceFacade");
	}
	
	
	private NeedReceiveRefundServiceFacade getNeedReceiveRefundServiceFacade(){
		return (NeedReceiveRefundServiceFacade)SpringWebContextUtil.getApplicationContext().getBean("needReceiveRefundServiceFacade");
	}
	
}
